List and Map types, respectively. They enable the simultaneous validation of a collection’s shape (length or key existence) and the destructuring of its elements into variables.
List Pattern
A list pattern matches an object if:- The object implements the
Listinterface. - The object’s length matches the number of subpatterns (unless a rest element is used).
- Each element in the list matches the corresponding subpattern at that position.
Syntax
The pattern is enclosed in square brackets[] and relies on positional matching.
Exact Length Matching
When no rest element is present, the pattern enforces a strict length check.Rest Element
The rest element (...) allows matching lists of arbitrary lengths. It matches any number of elements at its position.
- Unnamed Rest:
...ignores the remaining elements. - Named Rest:
...variableNamecaptures the remaining elements into a newList.
Map Pattern
A map pattern matches an object if:- The object implements the
Mapinterface. - The map contains all keys specified in the pattern.
- The values associated with those keys match their corresponding subpatterns.
Syntax
The pattern is enclosed in curly braces{}. It relies on key-based matching. Keys must be constant expressions (literals or const variables).
Key Matching and Destructuring
Map patterns require explicit keys mapping to subpatterns. There is no shorthand syntax (variable punning) for map patterns; the key must always be defined explicitly, even if the variable name matches the key.Recursive Composition
List and Map patterns are composable. A subpattern within a list or map pattern can be another list or map pattern, allowing for deep destructuring of nested data structures.Type Annotation in Patterns
Type annotations can be applied to the pattern itself or individual subpatterns to enforce type checks during matching.Master Dart with Deep Grasping Methodology!Learn More





